<?php

namespace App\Http\Requests;

use HttpResponseException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;

class UserPostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required|unique:users|max:85',
            'email' => 'email|unique:users',
            'password' => [Password::min(8)],
            'status' => 'required|in:1,2',
            'roleIds' => 'required|array',
            'roleIds.*' => 'integer',
            'token_expire' => 'integer',
            'departmentIds' => 'required|array',
            'departmentIds.*' => 'integer',
        ];
    }

    /**
     * 获取定义的验证规则的错误消息。
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.required' => '用户名不能为空',
            'name.unique' => '用户名已存在',
            'name.max' => '用户名最大长度为85',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '邮箱已存在',
            'password.required' => '密码不能为空',
            'password.min' => '密码最小长度为8',
            'status.required' => '状态不能为空',
            'status.in' => '状态值不正确',
            'role_ids.required' => '角色不能为空',
            'role_ids.array' => '角色格式不正确',
            'role_ids.*.integer' => '角色格式不正确',
            'token_expire.required' => 'token过期时间不能为空',
            'token_expire.integer' => 'token过期时间格式不正确',
        ];
    }

}
